package bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class FmMemberDTO {
	private FmMemberVO vo = null;
	private Connection conn = null;
	
	
	public FmMemberDTO(){
		this.conn = new FmMyOracle().getConn();
	}
	
	public boolean overlap() {		
		boolean b = false;
		
		String sql = "select mid from fmmember where mid = ?";
		
		try {
			PreparedStatement pst = this.conn.prepareStatement(sql);
		
			pst.setString(1, this.vo.getMid());
			
			ResultSet rs = pst.executeQuery();	
			
			while(rs.next()){
				b = true;
			}
			
			rs.close();
			pst.close();
			this.conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return b;
	}
	
	
	
	
	public boolean login() {
		boolean bolean = false;

		try {

			String sql = "select * from fmmember where mid=? and pwd=?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, this.vo.getMid());
			pst.setString(2, this.vo.getPwd());

			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				bolean = true;
			}
			pst.close();
			this.conn.close();
		} catch (Exception e) {

		}
		return bolean;
	}
	
	
	
	
	
	public boolean join(){
		boolean b = false;
		
		this.vo.setPhone(this.vo.getPn1() + "-" + this.vo.getPn2() + "-" + this.vo.getPn3());
		
		try {
			String sql = "insert into fmmember(mid, pwd, irum, email, phone, address, question, answer) values(?, ?, ?, ?, ?, ?, ?, ?)";
			PreparedStatement pst = conn.prepareStatement(sql);
			
			pst.setString(1, this.vo.getMid());
			pst.setString(2, this.vo.getPwd());
			pst.setString(3, this.vo.getIrum());
			pst.setString(4, this.vo.getEmail());
			pst.setString(5, this.vo.getPhone());
			pst.setString(6, this.vo.getAddress());
			pst.setString(7, this.vo.getQuestion());
			pst.setString(8, this.vo.getAnswer());
			
			int n = pst.executeUpdate();
			
			if(n > 0){
				b = true;
			}
			
			pst.close();
			this.conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;

	}

	
	
	public FmMemberVO select(){
		String sql = "select * from fmmember where mid = ?";
		
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());

			ResultSet rs = pst.executeQuery();
			
			while(rs.next()){
				this.vo.setIrum(rs.getString("irum"));
				this.vo.setPwd(rs.getString("pwd"));
				this.vo.setPhone(rs.getString("phone"));
				this.vo.setEmail(rs.getString("email"));
				this.vo.setAddress(rs.getString("address"));
				this.vo.setQuestion(rs.getString("question"));
				this.vo.setAnswer(rs.getString("answer"));
			}
			rs.close();
			pst.close();
			conn.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String[] str = this.vo.getPhone().split("-");
		
		this.vo.setPn1(str[0]);
		this.vo.setPn2(str[1]);
		this.vo.setPn3(str[2]);
		
		return this.vo;
	}
	
	
	public boolean modify(){
		boolean b = false;
		
		this.vo.setPhone(this.vo.getPn1() + "-" + this.vo.getPn2() + "-" + this.vo.getPn3());
		
		try {
			String sql = "update fmmember set pwd = ?, irum = ?, email = ?, phone = ?, address = ?, question = ?, answer = ? where mid = ?";
			PreparedStatement pst = conn.prepareStatement(sql);
			
			pst.setString(1, this.vo.getPwd());
			pst.setString(2, this.vo.getIrum());
			pst.setString(3, this.vo.getEmail());
			pst.setString(4, this.vo.getPhone());
			pst.setString(5, this.vo.getAddress());
			pst.setString(6, this.vo.getQuestion());
			pst.setString(7, this.vo.getAnswer());
			pst.setString(8, this.vo.getMid());
			
			int n = pst.executeUpdate();
			
			if(n > 0){
				b = true;
			}
			
			pst.close();
			this.conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;

	}
	
	
	public boolean delete() {
		boolean b = false;
		
		String sql = " delete from fmmember where mid = ?";
		
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, this.vo.getMid());
			
			int cnt = pst.executeUpdate();
			if(cnt > 0){
				b = true;	
			}
			
			pst.close();
			conn.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return b;
	}
	
	
	


	public FmMemberVO getVo() {
		return this.vo;
	}
	public void setVo(FmMemberVO vo) {
		this.vo = vo;
	}
		
}
